
clear
set more off

/*********************************************************************************
Name: verified_patients.do

Data In: [Data/Original/patient_surveys_attempts.dta,
		  Data/Original/patient_surveys.dta,
		  Data/Original/patient_start_dates.dta,
		  Data/Original/hw_roster.dta]

Data Out: [Data/Intermediate/verified_patients.dta]

Results Out: []

Purpose of do-file: Creating the list of verified patients
*********************************************************************************/

* Setting path directory
cd "${DIRECTORY}"


** Calling and merging datasets

use "Data/Original/patient_surveys_attempts.dta", clear

merge 1:1 q1_ques_code a02_entry_exit using "Data/Original/patient_surveys.dta"
drop _merge

merge m:1 q1_ques_code using "Data/Original/patient_start_dates.dta"
drop _merge

merge m:1 Unique_ID UID_Center using "Data/Original/hw_roster.dta"
keep if _merge == 3


** Creating variables

* Indicator of first attempt to survey
so q1_ques_code a02_entry_exit
by q1_ques_code: gen first_attempt = _n
replace first_attempt = 0 if first_attempt ~= 1

* Variable indicating that the patient was detected after the start of the experiment
gen post_exp = (treatment_start_date > expstartdate) if treatment_start_date ~= .

* Definition of verified patients  
so q1_ques_code
by q1_ques_code: egen verified_patient = max(survey_complete)

* Definition of first and last survey completed
so q1_ques_code survey_complete a02_entry_exit 
by q1_ques_code survey_complete: gen first_complete = _n
replace first_complete = 0 if first_complete ~= 1 | survey_complete ~= 1

gsort q1_ques_code survey_complete -a02_entry_exit 
by q1_ques_code survey_complete: gen last_complete = _n
replace last_complete = 0 if last_complete ~= 1 | survey_complete ~= 1

* Survey type
gen entry_survey = (a02_entry_exit == 1)


** Statistics for Figure 2

* Number of patients per treatment group
so q1_ques_code
by q1_ques_code: gen temp = _n
count if temp == 1 & verified_patient == 1
count if temp == 1 & verified_patient == 1 & treatment == 0
count if temp == 1 & verified_patient == 1 & treatment == 1

* Number of entry surveys per treatment group
count if entry_survey == 1 & survey_complete == 1
count if entry_survey == 1 & survey_complete == 1 & treatment == 0
count if entry_survey == 1 & survey_complete == 1 & treatment == 1

* Number of exit surveys per treatment group
count if entry_survey == 0 & survey_complete == 1
count if entry_survey == 0 & survey_complete == 1 & treatment == 0
count if entry_survey == 0 & survey_complete == 1 & treatment == 1

keep q1_ques_code a02_entry_exit verified_pat entry_survey post_exp first_attempt first_complete last_complete

save "Data/Intermediate/verified_patients.dta", replace
